﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DauGiaApp.Models;
namespace DauGiaApp.Controllers
{
    public class ManagerController : Controller
    {
        //
        // GET: /Manager/

        DauGiaEntities data = new DauGiaEntities();
        public bool IsRole(TaiKhoan tk, string actionName)
        {

            PhanQuyen pq = (from dspq in data.PhanQuyens where dspq.TenQuyen == actionName select dspq).SingleOrDefault();
            ChiTietPhanQuyen ctpq = (from ctpqs in data.ChiTietPhanQuyens where (ctpqs.MaLoaiTaiKhoan == tk.MaLoaiTaiKhoan) && (ctpqs.MaPhanQuyen == pq.MaPhanQuyen) select ctpqs).Single();
            if (ctpq != null)
                return true;
            return false;
        }
        public ActionResult Index()
        {
            TaiKhoan curTK = Session["user"] as TaiKhoan;
            if (curTK != null)
            {
                try
                {
                    if (IsRole(curTK, "Index"))
                    {
                        return View();
                    }
                    else
                    {
                        // Hoac thong bao thui.
                        return RedirectToAction("Alert", "Home");
                    }
                }
                catch
                {
                    //Response.Write("<script>alert('bạn không có quyền');</script>");
                    return RedirectToAction("Alert", "Home");
                }



            }
            else
            {
                return RedirectToAction("DangNhap", "TaiKhoan");
            }
            return RedirectToAction("Index", "Home");


        }
       
        public ActionResult ProductManage()
        {
            TaiKhoan curTK = Session["user"] as TaiKhoan;
            if (curTK != null)
            {
                try
                {
                    if (IsRole(curTK, "ProductManage"))
                    {
                        return View(data.SanPhams.ToList());
                    }
                    else
                    {
                        // Hoac thong bao thui.
                        return RedirectToAction("Alert", "Home");
                    }
                }
                catch
                {
                    //Response.Write("<script>alert('bạn không có quyền');</script>");
                    return RedirectToAction("Alert", "Home");
                }



            }
            else
            {
                return RedirectToAction("DangNhap", "TaiKhoan");
            }
            return RedirectToAction("Index", "Home");
            
        }

        public ActionResult ProductTypeManager()
        {
            TaiKhoan curTK = Session["user"] as TaiKhoan;
            if (curTK != null)
            {
                try
                {
                    if (IsRole(curTK, "ProductTypeManager"))
                    {
                        return View(data.LoaiSanPhams.ToList());
                    }
                    else
                    {
                        // Hoac thong bao thui.
                        return RedirectToAction("Alert", "Home");
                    }
                }
                catch
                {
                    //Response.Write("<script>alert('bạn không có quyền');</script>");
                    return RedirectToAction("Alert", "Home");
                }



            }
            else
            {
                return RedirectToAction("DangNhap", "TaiKhoan");
            }
            return RedirectToAction("Index", "Home");
            
            
        }
        public ActionResult CommentManage()
        {
            TaiKhoan curTK = Session["user"] as TaiKhoan;
            if (curTK != null)
            {
                try
                {
                    if (IsRole(curTK, "CommentManage"))
                    {
                        List<SanPham> query = (from sp in data.SanPhams
                                               where (sp.TinhTrangSanPham.MaTinhTrangSanPham == 3)
                                               select sp).ToList();
                        return View(query);
                    }
                    else
                    {
                        // Hoac thong bao thui.
                        return RedirectToAction("Alert", "Home");
                    }
                }
                catch
                {
                    //Response.Write("<script>alert('bạn không có quyền');</script>");
                    return RedirectToAction("Alert", "Home");
                }

            }
            else
            {
                return RedirectToAction("DangNhap", "TaiKhoan");
            }
            return RedirectToAction("Index", "Home");

        }

        public ActionResult DeleteComment(int id)
        {
            TaiKhoan curTK = Session["user"] as TaiKhoan;
            if (curTK != null)
            {
                try
                {
                    if (IsRole(curTK, "DeleteComment"))
                    {
                        var cm = (from c in data.Comments where c.MaComment == id select c).SingleOrDefault();
                        data.Comments.DeleteObject(cm);
                        data.SaveChanges();
                        return RedirectToAction("CommentManage", "Manager");
                    }
                    else
                    {
                        // Hoac thong bao thui.
                        return RedirectToAction("Alert", "Home");
                    }
                }
                catch
                {
                    //Response.Write("<script>alert('bạn không có quyền');</script>");
                    return RedirectToAction("Alert", "Home");
                }

            }
            else
            {
                return RedirectToAction("DangNhap", "TaiKhoan");
            }
            return RedirectToAction("Index", "Home");

        }

        [HttpPost]
        public ActionResult DangComment(int masanpham, string noidung_comment)
        {
            TaiKhoan curTK = Session["user"] as TaiKhoan;
            if (curTK != null)
            {
                try
                {
                    if (IsRole(curTK, "DangComment"))
                    {
                        Comment cm = new Comment();

                        cm.MaSanPham = masanpham;
                        cm.MaTaiKhoan = 1;
                        cm.TaiKhoan = data.TaiKhoans.ToList().ElementAt(0);
                        cm.NgayComment = DateTime.Now;
                        cm.NoiDungComment = noidung_comment;
                        //cm.MaComment= new Random().Next(1000);

                        data.Comments.AddObject(cm);
                        data.SaveChanges();
                        return RedirectToAction("GetComment", cm);
                    }
                    else
                    {
                        // Hoac thong bao thui.
                        return RedirectToAction("Alert", "Home");
                    }
                }
                catch
                {
                    //Response.Write("<script>alert('bạn không có quyền');</script>");
                    return RedirectToAction("Alert", "Home");
                }

            }
            else
            {
                return RedirectToAction("DangNhap", "TaiKhoan");
            }
            return RedirectToAction("Index", "Home");


        }
        public ActionResult CacLuotDauGiaCuaMotSanPham(string id)
        {
            if (id == null)
                return View();
            if (id.Length == 0)
                return View();
            int maSanPham;
            // if(id != null)
            maSanPham = int.Parse(id);
            var sanPham = from n in data.SanPhams
                          where n.MaSanPham == maSanPham
                          select n;
            if (sanPham.Count() > 0)
                return View(sanPham.ToList().ElementAt(0));
            else return View();
        }
        public ActionResult XoaSanPhamSaiQuyDinh(string id)
        {
            if (id == null) return View();
            int masanpham = int.Parse(id);
            var sp = from n in data.SanPhams
                     where n.MaSanPham == masanpham
                     select n;
            var ttsp = from n in data.TinhTrangSanPhams
                       where n.MaTinhTrangSanPham == 9
                       select n;

            try
            {
                if (ttsp.Count() > 0)
                {
                    var ttspItem = ttsp.ToList().ElementAt(0);
                    foreach (SanPham n in sp)
                    {
                        n.TinhTrangSanPham = ttspItem;

                    }
                }
                data.SaveChanges();
                Response.Write("<script>alert('xóa thành công');</script>");
                return RedirectToAction("ProductManage");
            }
            catch (Exception ex)
            {
                return View(sp.ToList().ElementAt(0));
            }
        }
        public ActionResult DanhSachSanPham()
        {
            return View(data.SanPhams);
        }
        public ActionResult GetComment(Comment comment)
        {
            TaiKhoan curTK = Session["user"] as TaiKhoan;
            if (curTK != null)
            {
                try
                {
                    if (IsRole(curTK, "GetComment"))
                    {
                        comment.TaiKhoan = data.TaiKhoans.ToList().ElementAt(0);
                        //Comment comment =  data.Comments.ToList().Find(cm => cm.MaComment == maComment);
                        return PartialView(comment);
                    }
                    else
                    {
                        // Hoac thong bao thui.
                        return RedirectToAction("Alert", "Home");
                    }
                }
                catch
                {
                    //Response.Write("<script>alert('bạn không có quyền');</script>");
                    return RedirectToAction("Alert", "Home");
                }

            }
            else
            {
                return RedirectToAction("DangNhap", "TaiKhoan");
            }
            return RedirectToAction("Index", "Home");
            
        }

           public ActionResult BaoCaoThongKe(ThongTinThongKe tk)
        {
            TaiKhoan curTK = Session["user"] as TaiKhoan;
            if (curTK != null)
            {
                try
                {
                    if (IsRole(curTK, "BaoCaoThongKe"))
                    {
                        List<SelectListItem> lthang = new List<SelectListItem>();
                        for (int i = 1; i < 13; i++)
                        {
                            lthang.Add(new SelectListItem { Text = "Tháng " + i.ToString(), Value = i.ToString() });
                        }
                        ViewData["ListThang"] = new SelectList(lthang, "Value", "Text");
                        var querysp = from sp in data.SanPhams select sp;
                        if (tk.thang > 0)
                        {
                            querysp = querysp.Where(a => a.NgayDang.Month == tk.thang);
                        }
                        if (tk != null)
                        {
                            querysp = querysp.Where(a => a.NgayDang.Year == tk.nam);
                        }
                        if (querysp != null)
                        {
                            List<SanPham> listsp = querysp.ToList();
                            int sogiaodich = 0;
                            int chiphidang = 2000;
                            foreach (SanPham sp in listsp)
                            {
                                if (sp.TinhTrangSanPham.MaTinhTrangSanPham == 3)
                                {
                                    sogiaodich++;
                                }
                            }
                            tk.SoLuongSanPham = querysp.Count();
                            tk.SoLuongGiaoDich = sogiaodich;
                            tk.DoanhThu = tk.SoLuongSanPham * chiphidang;
                        }
                        return View(tk);
                    }
                    else
                    {
                        // Hoac thong bao thui.
                        return RedirectToAction("Alert", "Home");
                    }
                }
                catch
                {
                    //Response.Write("<script>alert('bạn không có quyền');</script>");
                    return RedirectToAction("Alert", "Home");
                }

            }
            else
            {
                return RedirectToAction("DangNhap", "TaiKhoan");
            }
            return RedirectToAction("Index", "Home");
            
        }
        [HttpGet]
        public ActionResult BaoCaoThongKe()
        {
            TaiKhoan curTK = Session["user"] as TaiKhoan;
            if (curTK != null)
            {
                try
                {
                    if (IsRole(curTK, "BaoCaoThongKe"))
                    {
                        List<SelectListItem> lthang = new List<SelectListItem>();
                        for (int i = 1; i < 13; i++)
                        {
                            lthang.Add(new SelectListItem { Text = "Tháng " + i.ToString(), Value = i.ToString() });
                        }
                        ViewData["ListThang"] = new SelectList(lthang, "Value", "Text");
                        return View();
                    }
                    else
                    {
                        // Hoac thong bao thui.
                        return RedirectToAction("Alert", "Home");
                    }
                }
                catch
                {
                    //Response.Write("<script>alert('bạn không có quyền');</script>");
                    return RedirectToAction("Alert", "Home");
                }

            }
            else
            {
                return RedirectToAction("DangNhap", "TaiKhoan");
            }
            return RedirectToAction("Index", "Home");
            
        }
    }
}
